package com.youlin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youlin.dto.*;
import com.youlin.dto.req.EditAppReqDto;
import com.youlin.dto.req.FindAllAppReqDto;
import com.youlin.dto.res.AppInfoResDto;
import com.youlin.entities.*;
import com.youlin.mapper.BygAppInfoMapper;
import com.youlin.service.BygAppInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlin.service.BygAreaService;
import com.youlin.untils.BaseService;
import org.apache.commons.lang.BooleanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Random;

@Service
public class BygAppInfoServiceImpl extends ServiceImpl<BygAppInfoMapper, BygAppInfo> implements BygAppInfoService {

    @Autowired
    private BygAreaService bygAreaService;

    @Override
    public PageDataResDto<List<AppInfoResDto>> findAppInfos(BygAdmin currentAdmin,
                                           FindAllAppReqDto data, PageInfo pageInfo) {

        LambdaQueryWrapper<BygAppInfo> wrapper = Wrappers.lambdaQuery(BygAppInfo.class);
        Integer province = data.getProvince();
        if (province != null) wrapper.eq(BygAppInfo::getProvince, province);
        Integer city = data.getCity();
        if (city != null) wrapper.eq(BygAppInfo::getCity, city);
        String appName = data.getAppName();
        if (StringUtils.isNotBlank(appName)) wrapper.like(BygAppInfo::getAppName, appName);
        String appCode = data.getAppCode();
        if (StringUtils.isNotBlank(appCode)) wrapper.like(BygAppInfo::getAppCode, appCode);
        switch (data.getOrderBy()) {
            case 1:
                wrapper.orderByDesc(BygAppInfo::getCreateTime);
                break;
            case 2:
                wrapper.orderByDesc(BygAppInfo::getAmCount);
                break;
            default: //3
                wrapper.orderByDesc(BygAppInfo::getFhCount);
                break;
        }

        Page<BygAppInfo> page = page(new Page<>(pageInfo.getPage(), pageInfo.getSize()), wrapper);
        List<AppInfoResDto> appInfoResDtos = BaseService.findAndDealWithOneToOne(page.getRecords(),
                BygAppInfo::getProvince, AppInfoResDto.class, bygAreaService, BygArea::getAreaId);
        appInfoResDtos = BaseService.findAndDealWithOneToOne(appInfoResDtos,
                AppInfoResDto::getCity, AppInfoResDto.class, bygAreaService, BygArea::getAreaId);

        return new PageDataResDto<List<AppInfoResDto>>(Status.SUCCESS)
                .setData(appInfoResDtos).setPageInfo(pageInfo.setTotal(page.getTotal()));
    }

    @Override
    public BaseResDto<Status, String> editAppInfo(EditAppReqDto data) {
        BygAppInfo bygAppInfo = BaseService.copyProperties(data, BygAppInfo.class);

        if (BooleanUtils.isTrue(data.getIsDel())) {
            if (data.getAppId() == null) return new BaseResDto<>(Status.PARAMETERERROR);
            if (! removeById(data.getAppId())) return new BaseResDto<>(Status.ERROR);
            return new BaseResDto<>(Status.SUCCESS);
        }

        if (data.getAppId() == null) { // 新建单位
            String cityCode = data.getCity().toString();
            for (int i = 1; i <= 20; i++) {
                int random = new Random().ints(100000, 999999).findAny().getAsInt();
                String appCode = cityCode + random;
                LambdaQueryWrapper<BygAppInfo> wrapper = Wrappers
                        .lambdaQuery(BygAppInfo.class).eq(BygAppInfo::getAppCode, appCode);
                if (count(wrapper) == 0) {
                    bygAppInfo.setAppCode(appCode);
                    break;
                }
            }
            if (StringUtils.isBlank(bygAppInfo.getAppCode())) return new BaseResDto<>(Status.ERROR);
            bygAppInfo.setCreateTime(LocalDateTime.now());
        }

        if (! saveOrUpdate(bygAppInfo)) return new BaseResDto<>(Status.ERROR);
        return new BaseResDto<>(Status.SUCCESS);
    }

    @Override
    public BaseDataResDto<AppInfoResDto> findAppInfoById(Integer data) {
        BygAppInfo appInfo = getById(data);
        if (appInfo == null) return new BaseDataResDto<>(Status.PARAMETERERROR);
        AppInfoResDto appInfoResDto = BaseService.copyProperties(appInfo, AppInfoResDto.class);
        return new BaseDataResDto<AppInfoResDto>(Status.SUCCESS).setData(appInfoResDto);
    }

}
